package br.com.salao.session;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.FavoritosDao;
import br.com.salao.ejb.dao.FuncionarioDao;
import br.com.salao.ejb.dao.impl.FavoritosDaoImpl;
import br.com.salao.ejb.dao.impl.FuncionarioDaoImpl;
import br.com.salao.entity.Favoritos;

@Stateless
@Remote(FavoritosBase.class)
public class FavoritosBean implements FavoritosBase {

	/**
	 * @author Dri
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(FavoritosBean.class.getName());

	@PersistenceContext(unitName = "SalaoBeleza")
	private EntityManager em;

	public FavoritosBean(){
		logger.info("Construtor FavoritosBean");
	}

	@TransactionAttribute
	public Favoritos incluir(Favoritos favoritos) throws Exception{
		FavoritosDao dao = new FavoritosDaoImpl(em);
		return dao.incluir(favoritos);		
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public List<Favoritos> buscarFavoritosPorLogin(int idLogin) throws Exception{
		FavoritosDao dao = new FavoritosDaoImpl(em);
		return dao.buscarFavoritosPorLogin(idLogin);		
	}

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public Favoritos buscarFavoritosPorUsuarioEEstabelecimento(int idLogin, int idEstabelecimento) throws Exception{
		FavoritosDao dao = new FavoritosDaoImpl(em);
		return dao.buscarFavoritosPorLoginEEstabelecimento(idLogin, idEstabelecimento);		
	}

	@TransactionAttribute
	public void excluirFavorito(int idLogin, int idEstabelecimento) throws Exception{

		FavoritosDao dao = new FavoritosDaoImpl(em);

		try{						
			dao.excluirFavorito(idLogin, idEstabelecimento);

		}catch (Exception e) {

			StringBuffer sb = new StringBuffer();
			sb.append(e.getStackTrace() + "\n");
			sb.append(e.getCause() + "\n");
			sb.append(e.getMessage() + "\n");			
			logger.error(sb.toString());
			throw e;
		}

	}

}
